﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Common;
using System.Json;



namespace WebManager.Views
{
    public partial class Account : Common.SLBasePage
    {
        public Account()
        {
            InitializeComponent();

            acctDataGrid.DataBindingCompleted += new Common.SLDataGrid.DataBindingCompletedHandler(dbObjDataGrid_DataBindingCompleted);
            acctDataGrid.Tag = "get_acct_list";
            acctDataGrid.IsCustomerBinding = true;
            acctDataGrid.CustomerBinding += new SLDataGrid.CustomerBindingHandler(acctDataGrid_CustomerBinding);
            acctDataGrid.DataGrid.AutoGenerateColumns = true;
            acctDataGrid.DataGrid.RowDetailsTemplate = Resources["DetailTemplate"] as DataTemplate;
            acctDataGrid.DataGrid.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.VisibleWhenSelected;
            
            acctDataGrid.DataGrid.RowDetailsVisibilityChanged += new EventHandler<DataGridRowDetailsEventArgs>(DataGrid_RowDetailsVisibilityChanged);
            acctDataGrid.DataGrid.IsReadOnly = true;
            acctDataGrid.FillDataGrid();
        }

        
        void acctDataGrid_CustomerBinding(string jsonData)
        {
            JsonObject jObject = (JsonObject)JsonObject.Parse(jsonData);
            if (!jObject.ContainsKey("column_header"))
                return;

            JsonArray jArray = (JsonArray)jObject["column_header"];
            int col_cnt = jArray.Count;

            var rows = new System.Collections.ObjectModel.ObservableCollection<Row>();
            if (!jObject.ContainsKey("recordset"))
                return;

            List<AccountInfo> accountList = new List<AccountInfo>();
            jArray = (JsonArray)jObject["recordset"];
            for (int i = 0; i < jArray.Count; i++)
            {
                AccountInfo row = new AccountInfo();
                JsonArray jSubArray = (JsonArray)jArray[i];
                row.ID = jSubArray[0].ToString().Replace("\"", "");
                row.Name = jSubArray[1].ToString().Replace("\"", "");
                row.Password = jSubArray[2].ToString().Replace("\"", "");
                row.Type = jSubArray[3].ToString().Replace("\"", "");
                row.Status = jSubArray[4].ToString().Replace("\"", "");
                row.RegisterdDate = jSubArray[5].ToString().Replace("\"", "");
                accountList.Add(row);
            }
            acctDataGrid.DataGrid.ItemsSource = accountList;
            acctDataGrid.DataGrid.CanUserSortColumns = true;
        }

        void DataGrid_RowDetailsVisibilityChanged(object sender, DataGridRowDetailsEventArgs e)
        {
            Grid objGrid = ((Grid)e.DetailsElement);
            Common.SLDataGrid svcGrid = objGrid.Children[0] as Common.SLDataGrid;
            if (svcGrid == null)
                return;

            AccountInfo acct = (AccountInfo)e.Row.DataContext;
            if (acct == null)
                return;

            svcGrid.Tag = "get_acct_detail";
            svcGrid.Params.Clear();
            svcGrid.Params.Add(acct.ID);
            svcGrid.FillDataGrid();
        }

        public void ReloadAccount()
        {
            acctDataGrid.FillDataGrid();
        }
       
        // 사용자가 이 페이지를 탐색할 때 실행됩니다.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

    

        void dbObjDataGrid_DataBindingCompleted()
        {
            //this.acctDataGrid.DataGrid.Columns[0].Width = new DataGridLength(150);
            //this.acctDataGrid.DataGrid.Columns[1].Width = new DataGridLength(150);
            //this.acctDataGrid.DataGrid.Columns[3].Width = new DataGridLength(100);
            //this.acctDataGrid.DataGrid.Columns[4].Width = new DataGridLength(100);
            //this.acctDataGrid.DataGrid.Columns[5].Width = new DataGridLength(100);
        }

        private void Add_Button_Click(object sender, RoutedEventArgs e)
        {
            ChildWindow1 child = new ChildWindow1(this);
            child.Show();
        }

        private void Delete_Button_Click(object sender, RoutedEventArgs e)
        {
            AccountInfo row = this.acctDataGrid.DataGrid.SelectedItem as AccountInfo;
            if (row != null)
            {
                string acct_id = row.ID;
                Common.CommonAPI.InvokeWebAPIByPost(this.uploadStringCompleted, "delete_account", acct_id, null);
            }

        }

        protected override void webClient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;

            Result result = CommonAPI.GetResut(e.Result);
            if (!result.result)
            {
                // Error 처리
                //SL_Meta.Ctrls.MainPage.msgBox.ShowMsgBox(result.error);
                return;
            }

            switch (result.command)
            {
                case "delete_account":
                    {
                        MessageBox.Show("계정이 삭제되었습니다.");
                        acctDataGrid.FillDataGrid();
                    }
                    break;


                default:
                    break;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            acctDataGrid.FillDataGrid();
        }

    }

    public class AccountInfo
    {
        public string ID
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public string Password
        {
            get;
            set;
        }

        public string Type
        {
            get;
            set;
        }

        public string Status
        {
            get;
            set;
        }

        public string RegisterdDate
        {
            get;
            set;
        }
    }
}
